package xgenerator.ui.swing.adapter;

import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Icon;
import javax.swing.JOptionPane;

import xgenerator.model.ModelMetadata;
import xgenerator.ui.swing.JGeneratorFrame;
import xgenerator.ui.swing.MetadataTable;
import xgenerator.ui.swing.model.MetadataTableModel;
import xgenerator.util.NotationUtils;
import xgenerator.util.StringUtils;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see 
 * @since   1.0
 */
public class MetadataAdapter extends AbstractAction {
	
	/**
	 * Serializable
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 查询actionCommand
	 */
	public static final String QUERY_METADATA_COMMAND = "QUERY_METADATA_COMMAND";

	/**
	 * UI界面
	 */
	private Component ui;

	/**
	 * constructor
	 * @param ui
	 */
	public MetadataAdapter(Component ui) {
		this(ui, null);
	}
	
	/**
	 * constructor
	 * @param ui
	 * @param name
	 */
	public MetadataAdapter(Component ui, String name) {
		this(ui, name, null);
	}
	
	/**
	 * constructor
	 * @param ui
	 * @param name
	 * @param icon
	 */
	public MetadataAdapter(Component ui, String name, Icon icon) {
		super(name, icon);
		this.ui = ui;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent e) {
		if(QUERY_METADATA_COMMAND.equals(e.getActionCommand())) {
			queryMetadata(e);
		}
	}
	
	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param e
	 */
	public void queryMetadata(ActionEvent e) {
		JGeneratorFrame generatorGui = (JGeneratorFrame) ui;

		try {
			Object dataSourceNameObj = generatorGui.getDataSourceComboBox().getSelectedItem();
			Object tableNameObj = generatorGui.getTableComboBox().getSelectedItem();
			String dataSourceName = null != dataSourceNameObj ? dataSourceNameObj.toString() : null;
			String tableName = null != tableNameObj ? tableNameObj.toString() : null;
			System.out.println("dataSourceName=" + dataSourceName + " | tableName=" + tableName);
			if(StringUtils.isBlank(dataSourceName)) {
				throw new RuntimeException("请选择数据源！");
			}
			if(StringUtils.isBlank(tableName)) {
				throw new RuntimeException("请选择数据表！");
			}

			generatorGui.getStatusLabel().setText("数据源:" + dataSourceName + " 模型:" + tableName + " 正在加载元数据...");
			
			MetadataTable metadataTable = generatorGui.getMetadataTable();
			MetadataTableModel metadataTableModel = (MetadataTableModel) metadataTable.getModel();
			/*
			 * 必须先设置TableModel，然后再设置TableColumnModel 在这里
			 */
//			metadataTable.setModel(metadataTableModel); //在这里metadataTableModel没有变
//			metadataTable.setColumnModel(metadataTable.setPreferredTableColumnStyle());
			
			ModelMetadata modelMetadata = metadataTableModel.updateDataVector(dataSourceName, tableName);
			metadataTable.setPreferredTableColumnStyle();
			metadataTable.revalidate();
			metadataTable.updateUI();

			String filterTableNamePrefix = generatorGui.getFilterTableNamePrefixField().getText();
			String filterTableNameSuffix = generatorGui.getFilterTableNameSufferField().getText();
//			String filterColumnNamePrefix = generatorGui.getFilterColumnNamePrefixField().getText();
			
			generatorGui.getMetadataModelNameField().setText(tableName);
			generatorGui.getJavaModelNameField().setText(NotationUtils.upperCamelCase(tableName, NotationUtils.DEFAULT_NOTATION_SEPARATOR, filterTableNamePrefix, filterTableNameSuffix));
			generatorGui.getModelDescriptionField().setText(modelMetadata.getModelDescription());
			generatorGui.getStatusLabel().setText("数据源:" + dataSourceName + " 模型:" + tableName + " 加载元数据成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			generatorGui.getStatusLabel().setText(ex.getMessage());
			JOptionPane.showMessageDialog(ui, ex.getMessage());
		}
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	public Component getUI() {
		return ui;
	}

	/**
	 * <p>
	 * Title：
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param ui
	 */
	public void setUI(Component ui) {
		this.ui = ui;
	}

}
